package com.timoon.manager.panel.product.cell;

import java.util.ArrayList;
import java.util.List;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.ajax.markup.html.form.AjaxCheckBox;
import org.apache.wicket.ajax.markup.html.navigation.paging.AjaxPagingNavigator;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.PageableListView;
import org.apache.wicket.model.Model;
import org.apache.wicket.spring.injection.annot.SpringBean;

import com.timoon.manager.auth.BaseAuthPanel;
import com.timoon.manager.panel.product.ProductDetailPanel;
import com.timoon.sample.dao.model.Product;
import com.timoon.sample.dao.model.ProductSort;

/**
 *<p>功能:</p>
 *<p></p>
 *<p>Copyright:Copyright(c)2013</p>
 *<p>Company:sky</p>
 *@author 付乐天
 *@version 1.0 */

public class ProductGridCellPanel extends BaseAuthPanel {
	
	@SpringBean(name="manager.product")
	private ProductGridCellService service;
	
	List<String> checkboxSelectedId=new ArrayList<>();

	public ProductGridCellPanel(String id) {
		super(id);
		// TODO Auto-generated constructor stub
		
		renderPanel();
	}

	private void renderPanel() {
		// TODO Auto-generated method stub
		
		renderGrid();
		
	}

	private void renderGrid() {
		// TODO Auto-generated method stub
		
		List<Product> products=this.service.getAllProduct();
		
		Form form=new Form<>("wk_form");
		add(form);
		
		PageableListView<Product> listView=new PageableListView<Product>("wk_list",products,10) {
			
			@Override
			protected void populateItem(ListItem<Product> arg0) {
				// TODO Auto-generated method stub
				
				final Product product=arg0.getModelObject();
				
				arg0.add(new AjaxCheckBox("wk_checkbox",new Model<Boolean>(false)) {

					@Override
					protected void onUpdate(AjaxRequestTarget arg0) {
						// TODO Auto-generated method stub
						if((boolean) this.getDefaultModelObject()){
							checkboxSelectedId.add(product.getId().toString());
						}else{
							if(checkboxSelectedId.contains(product.getId().toString())){
								checkboxSelectedId.remove(product.getId().toString());
							}
						}
						
					}
					
				});
				
				arg0.add(new Label("wk_id",product.getId()));
				
				arg0.add(new Label("wk_name",product.getName()));
				
				arg0.add(new Label("wk_market_price",product.getMarketPrice()));
				
				arg0.add(new Label("wk_actual_price",product.getActualPrice()));
				
				ProductSort sort=service.getSortByProductId(product.getSortId());
				arg0.add(new Label("wk_sort",sort.getName()));
				
			}
		};
		add(listView);
		
		AjaxPagingNavigator navigation=new AjaxPagingNavigator("wk_paging",listView);
		form.add(navigation);
	}

}
